<?php namespace Core\Myapp\Helper;

use Core\Myapp\Log\Logger;

class Module
{
    protected $_modules = array();

    protected function getCommunityPaths()
    {
        $modulePaths = array();
        foreach (new \DirectoryIterator(MODULE_PATH . DS . 'community') as $fileInfo) {
            if($fileInfo->isDir())
                $modulePaths[$fileInfo->getBasename()] = $fileInfo->getPathname();
        }
        return $modulePaths;
    }
    protected function getCorePaths()
    {
        $modulePaths = array();
        foreach (new \DirectoryIterator(MODULE_PATH . DS . 'core') as $fileInfo) {
            if($fileInfo->isDir())
                $modulePaths[$fileInfo->getBasename()] = $fileInfo->getPathname();
        }
        return $modulePaths;
    }
    protected function setModules()
    {
        $community = $this->getCommunityPaths();
        $core = $this->getCorePaths();
        $modules = array_merge($community, $core);
        foreach($modules as $key=>$value)
        {
            $configFile = $value . DS . 'config.xml';
            if(!file_exists($configFile)) continue;
            if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $key)) continue;
            try {
                $config = simplexml_load_file($configFile);
                $name = $config->name;
                $author = $config->author;
                $version = $config->version;
                $description = $config->description;
                $website = $config->website;
                $route = $config->route;
                $events = $config->events;
                if(!$name)
                {
                    Logger::write('Name is missing in config file of module' . $key);
                    continue;
                }
                if(!$route)
                {
                    Logger::write('Route is missing in config file of module' . $key);
                    continue;
                }
                $module = new \Core\Myapp\Model\Entity\Module();
                $module->name = $name;
                $module->route = $route;
                $module->author = $author;
                $module->description = $description;
                $module->version = $version;
                $module->website = $website;
                $moduleevents = array();
                if(is_array($events)) {
                    foreach($events as $event) {
                        $term = new \stdClass();
                        $term->name = $event->event_name->__toString();
                        $term->class = $event->class_name->__toString();
                        $term->method = $event->method_name->__toString();
                        $term->priority = $event->priority->__toString();
                        $moduleevents[] = $term;
                    }
                } else {
                    $term = new \stdClass();
                    $term->name = $events->event->event_name->__toString();
                    $term->class = $events->event->class_name->__toString();
                    $term->method = $events->event->method_name->__toString();
                    $term->priority = $events->event->priority->__toString();
                    $moduleevents[] = $term;
                }
                $module->events = $moduleevents;
                $this->_modules[] = $module;
            }
            catch (\Exception $e)
            {

            }
        }
    }
    public function getModules()
    {
        if(!$this->_modules)
            $this->setModules();
        return $this->_modules;
    }
}